<template>
  <div class="xtx-city" ref="target">
    <div class="select" @click="openOption" :class="{ active: isShow }">
      <span class="placeholder">{{ cityStr }}</span>
      <span class="value"></span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-if="isShow">
      <template v-if="!loading">
        <span
          class="ellipsis"
          @click="clickCity(i)"
          v-for="i in city"
          :key="i.code"
          >{{ i.name }}</span
        >
      </template>
      <div class="loading" v-else></div>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue'
import { onClickOutside } from '@vueuse/core'
import { getCitys } from '@/api/goods'
export default {
  name: 'XtxCity',
  props: {
    cityStr: {
      type: String,
      default: '北京市 市辖区 朝阳区'
    }
  },
  setup (props, { emit }) {
    // 地址框的显示隐藏
    const isShow = ref(false)
    // 是否显示加载动画
    const loading = ref(false)

    // 获取到的省市区数组信息
    const city = ref([])

    // 获取到的省市区数组信息 备份
    const initCity = ref([])

    const target = ref(null)

    //  点击target外触发
    onClickOutside(target, event => {
      close()
    })

    // 显示选择框
    function openOption () {
      isShow.value = true
      if (city.value.length) {
        return
      }
      loading.value = true

      getCitys().then(res => {
        city.value = res
        initCity.value = res
        loading.value = false
      })
    }

    // 关闭选择框
    function close () {
      isShow.value = false
      city.value = initCity.value
      pickCity = {
        provinceName: '',
        provinceCode: '',
        cityName: '',
        cityCode: '',
        countyName: '',
        countyCode: '',
        fullLocation: ''
      }
    }

    // 选择省市区数据
    let pickCity = {
      provinceName: '',
      provinceCode: '',
      cityName: '',
      cityCode: '',
      countyName: '',
      countyCode: '',
      fullLocation: ''
    }

    // 点击选择城市
    function clickCity (i) {
      switch (i.level) {
        case 0:
          city.value = i.areaList
          pickCity.provinceName = i.name
          pickCity.provinceCode = i.code
          break
        case 1:
          city.value = i.areaList
          pickCity.cityName = i.name
          pickCity.cityCode = i.code
          break
        case 2:
          pickCity.countyName = i.name
          pickCity.countyCode = i.code
          pickCity.fullLocation = `${pickCity.provinceName} ${pickCity.cityName} ${pickCity.countyName} `
          emit('change', pickCity.fullLocation)
          emit('update:cityStr', pickCity.fullLocation)
          close()
          break

        default:
          break
      }
    }
    return { openOption, isShow, loading, target, city, clickCity }
  }
}
</script>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    .loading {
      height: 290px;
      width: 100%;
      background: url(~@/assets/images/loading.gif) no-repeat center;
    }
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
